module Resources
  class Logins < Grape::API
    class TokenEntity < Grape::Entity
      <%_ if (swagger) { -%>
      expose :token, documentation: { type: String, desc: '用以验证用户身份的 API token' }
      <%_ } else { -%>
      expose :token
      <%_ } -%>
    end

    resource :logins do
      <%_ if (swagger) { -%>
      desc '登录',
           entity: TokenEntity
      <%_ } -%>
      params do
        <%_ if (swagger) { -%>
        requires :id, type: Integer, desc: '使用用户 id 登录'
        <%_ } else { -%>
        requires :id, type: Integer
        <%_ } -%>
      end
      post do
        user = User.find(params[:id])

        status 200
        return { token: user.to_token }
      end
    end
  end
end
